﻿using System.Linq;
using System.Web.Mvc;
using TavDom.Library.Business.Concrete;
using TavDom.Library.DataAccess.Concrete;
using TavDom.Library.Entities.ComplexTypes;
using TavDom.Library.Entities.Concrete;
using TavDom.Library.UI.MVC.Models;

namespace TavDom.Library.UI.MVC.Controllers
{
   // [Authorize(Roles = "Geliştirici")]
    public class ContentTypeController : Controller
    {

        readonly ContentTypeManager _manager = new ContentTypeManager(new EfContentTypeDal());
        public int PageSize = 5;
        //protected override void OnAuthorization(AuthorizationContext filterContext)
        //{
        //    if (Session["role"] != null)
        //    {
        //        var genericPrincipal = (GenericPrincipal)Session["role"];
        //        System.Web.HttpContext.Current.User = genericPrincipal;
        //    }
        //    base.OnAuthorization(filterContext);
        //}
        public ViewResult Index(int page = 1)
        {

            var list = _manager.GetAllWithUsers();

            var listViewModel = new ListViewModel<ContentType>
            {
                ListModelEntities = list.OrderBy(e => e.Sort).Skip((page - 1) * PageSize).Take(PageSize).ToList(),
                Paging = new Paging
                {
                    ItemsPerPage = PageSize,
                    TotalItem = list.Count(),
                    CurrentPage = page,
                    IsSearch = false
                }
            };
            return View(listViewModel);

        }


        [HttpPost]
        public ViewResult Index(SearchEntity<ContentType> searchEntity, int page = 1)
        {
            //Arama yaptıktan sonra textboxların yeniden doldurulması yapılıyor
            ViewBag.page = page;
            ViewBag.Name = searchEntity.Name;
            ViewBag.CreateDateBegin = searchEntity.CreateDateBegin != null ? searchEntity.CreateDateBegin.Value.ToString("dd.MM.yyyy") : null;
            ViewBag.CreateDateEnd = searchEntity.CreateDateEnd != null ? searchEntity.CreateDateEnd.Value.ToString("dd.MM.yyyy") : null;
            ViewBag.UpdateDateBegin = searchEntity.UpdateDateBegin != null ? searchEntity.UpdateDateBegin.Value.ToString("dd-MM-yyyy") : null;
            ViewBag.UpdateDateEnd = searchEntity.UpdateDateEnd != null ? searchEntity.UpdateDateEnd.Value.ToString("dd-MM-yyyy") : null; 
            ViewBag.IsApproved = searchEntity.IsApproved;
            var statusSearchList = _manager.Search(searchEntity);

            var searchModel = new ListViewModel<ContentType>
            {
                ListModelEntities = statusSearchList.OrderBy(e => e.Sort).Skip((page - 1) * PageSize).Take(PageSize).ToList(),
                Paging = new Paging
                {
                    ItemsPerPage = PageSize,
                    TotalItem = statusSearchList.Count(),
                    CurrentPage = page,
                    IsSearch = true
                },

                SearchEntities = searchEntity
            };
            return View(searchModel);

        }

    
    }
}